package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.*;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.options.ChoiceOptionsService;
import org.example.course.service.AssignmentQuestionsService;
import org.example.course.service.CourseSonChaptersTestService;
import org.example.course.service.StudentTestAnswerService;
import org.example.course.service.StudentTestService;
import org.example.course.until.CourseUntil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 章节测试
 */
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseSonChaptersTestController {

    @Resource
    private CourseSonChaptersTestService courseSonChaptersTestService;

    @Resource
    private ChoiceOptionsService choiceOptionsService;

    @Resource
    private StudentTestAnswerService studentTestAnswerService;

    @Resource
    private StudentTestService studentTestService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 获取章节测试列表
     * @return
     */
    @GetMapping("/list/sonchapters/test/{chapterId}")
    public R listSonChaptersTest(@PathVariable Long chapterId){
        LambdaQueryWrapper<CourseSonChaptersTest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSonChaptersTest::getChapterId,chapterId);
        courseSonChaptersTestService.list(queryWrapper);
        return R.success();
    }


    /**
     * 添加章节测试
     * @param courseSonChaptersTest
     * @return
     */
    @PostMapping("/insert/sonchapters/test")
    public R insertSonChaptersTest(@RequestBody CourseSonChaptersTest courseSonChaptersTest){
        R result= CourseUntil.isSonChaptersTest(courseSonChaptersTest);
        if(result!=null)return result;
        courseSonChaptersTestService.save(courseSonChaptersTest);
        return R.success();
    }

    /**
     * 修改章节测试
     * @param courseSonChaptersTest
     * @return
     */
    @PutMapping("/update/sonchapters/test")
    public R updateSonChaptersTest(@RequestBody CourseSonChaptersTest courseSonChaptersTest){
        R result= CourseUntil.isSonChaptersTest(courseSonChaptersTest);
        if(result!=null)return result;
        courseSonChaptersTestService.updateById(courseSonChaptersTest);
        return R.success();
    }

    /**
     * 删除章节测试
     * @param id
     * @return
     */
    @DeleteMapping("/delete/sonchapters/test/{id}")
    public R deleteSonChaptersTest(@PathVariable Long id){
        courseSonChaptersTestService.removeById(id);
        return R.success();
    }

    /**
     * 插入章节检测题目
     * @param id
     * @param courseSonChaptersTests
     * @return
     */
    @PostMapping("/insert/sonchapters/test/questions/{id}")
    public R insertSonChaptersTestQuestions(@PathVariable Long id,@RequestBody List<CourseSonChaptersTest> courseSonChaptersTests){
        for(CourseSonChaptersTest assignmentQuestion:courseSonChaptersTests){
            assignmentQuestion.setChapterId(id);
        }
        courseSonChaptersTestService.saveBatch(courseSonChaptersTests);
        List<Long> ids=courseSonChaptersTests.stream().map(CourseSonChaptersTest::getId).collect(Collectors.toList());
        return R.success(ids);
    }

    /**
     * 获取章节检测题目
     * @param chapterId
     * @return
     */
    @GetMapping("/list/sonchapters/test/questions/{chapterId}")
    public R listSonChaptersTestQuestions(@PathVariable Long chapterId){
        LambdaQueryWrapper<CourseSonChaptersTest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSonChaptersTest::getChapterId,chapterId);
        List<CourseSonChaptersTest> list=courseSonChaptersTestService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 插入选项
     * @param choiceOptions
     * @return
     */
    @PostMapping("/insert/sonchapters/test/questions/options")
    public R insertSonChaptersTestQuestionsOptions(@RequestBody List<ChoiceOptions> choiceOptions){
        choiceOptionsService.saveBatch(choiceOptions);
        return R.success();
    }

    /**
     * 获取选项
     * @param id
     * @return
     */
    @GetMapping("/get/sonchapters/test/questions/options/{id}")
    public R getSonChaptersTestQuestionsOptions(@PathVariable Long id){
        LambdaQueryWrapper<ChoiceOptions> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ChoiceOptions::getChapterTestId,id);
        ChoiceOptions choiceOptions=choiceOptionsService.getOne(queryWrapper);
        return R.success(choiceOptions);
    }

    /**
     * 章节检测提交答案
     * @param answers
     * @return
     */
    @PostMapping("/insert/test/submitAnswer")
    public R insertTestSubmitAnswer(@RequestBody List<StudentTestAnswer> answers){
        studentTestAnswerService.saveBatch(answers);
        return R.success();
    }


    /**
     * 章节检测提交(返回给前端id)
     * @param chapterId
     * @param request
     * @return
     */
    @PostMapping("/submit/test/{chapterId}")
    public R submitTest(@PathVariable Long chapterId, HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        StudentTest studentTest=new StudentTest();
        studentTest.setChapterId(chapterId);
        studentTest.setUserId(loginInfo.getId());
        studentTestService.save(studentTest);
        return R.success(studentTest.getId());
    }


    /**
     * 学生是否完成章节测试
     * @param chapterId
     * @param request
     * @return
     */
    @GetMapping("/get/test/finish/{chapterId}")
    public R getTestFinish(@PathVariable Long chapterId,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        LambdaQueryWrapper<StudentTest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTest::getUserId,loginInfo.getId());
        queryWrapper.eq(StudentTest::getChapterId,chapterId);
        StudentTest studentTest=studentTestService.getOne(queryWrapper);
        return R.success(studentTest);
    }

    /**
     * 获取学生章节测试答案
     * @param chapterId
     * @param request
     * @return
     */
    @GetMapping("/list/test/student/answer/{chapterId}")
    public R listTestStudentAnswer(@PathVariable Long chapterId,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
//        查询学生的完成记录
        LambdaQueryWrapper<StudentTest> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTest::getUserId,loginInfo.getId());
        queryWrapper.eq(StudentTest::getId,chapterId);
        StudentTest studentTest=studentTestService.getOne(queryWrapper);
//        查询学生的完成答案
        LambdaQueryWrapper<StudentTestAnswer> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(StudentTestAnswer::getStudentTestId,studentTest.getId());
        List<StudentTestAnswer> list=studentTestAnswerService.list(queryWrapper1);
        return R.success(list);
    }

    /**
     * 获取学生章节测试中哪些提交过
     * @param request
     * @return
     */
    @GetMapping("/list/test/isSubmit/{courseId}")
    public R listTestIsSubmit(@PathVariable Long courseId,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        List<StudentTest> list=studentTestService.listIsSubmit(courseId,loginInfo.getId());
        return R.success(list);
    }

}
